package hk.edu.cityu.is.riskmgmt.algorithm.interbank;

import org.apache.log4j.Logger;

import java.util.Random;


public class InterbankMatrixConstructor {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(InterbankMatrixConstructor.class);

//	public static void main(String [] args){
//		Random r=new Random();
//		for(int i=0;i<1000;i++)
//			System.out.println(r.nextGaussian()+"\t");
//		
//	}
	public void setInterbankMatrix(int bankNO,int[][] InterbankMatrix){
		Random rand = new Random();
		
		for(int i=0;i<bankNO;i++){
			int j=i;
			while(!((j!=i)&&(InterbankMatrix[i][j]!=1)&&(InterbankMatrix[j][i]!=1))){
				j=rand.nextInt(bankNO);
			} 
			InterbankMatrix[i][j]=1;
		}
		
		for(int i=0;i<bankNO;i++){
			int j=i;
			while(!((j!=i)&&(InterbankMatrix[i][j]!=1)&&(InterbankMatrix[j][i]!=1))){
				j=rand.nextInt(bankNO);
			} 
			InterbankMatrix[j][i]=1;
		}
		
		int linkNO=bankNO*(bankNO-1)/2-bankNO*2;
		int i=linkNO,j,k;
		while(i>0)
		{
			j=rand.nextInt(bankNO);k=rand.nextInt(bankNO);
			if((j!=k)&&(InterbankMatrix[j][k]!=1)&&(InterbankMatrix[k][j]!=1))
			{
				InterbankMatrix[j][k]=1;
				i--;
			}
		}	
	}
}
